﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using Infragistics.Controls.Menus;
using StartupSolution.ViewModel;

namespace StartupSolution.Views.Menus.BuildingDataTree
{
    public partial class DataTreeCodeBehind : Page
    {
        XamDataTree MyTree = new XamDataTree();
        public DataTreeCodeBehind()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Sample_Loaded);
        }

        void Sample_Loaded(object sender, RoutedEventArgs e)
        {
            LoadXMLData();
        }

        /// <summary> 
        /// This method will load data from a XML document and populate it as XamGrid ItemSource
        /// </summary>
        private void LoadXMLData()
        {
            // 	Loads the XML file and creates a XDocument object
            XDocument doc = XDocument.Load("Datasource/Books.xml");

            // Run LINQ query to retrieve the necessary data


            var dataSource = (from d in doc.Descendants("book")
                              where (d.Descendants("chapter") != null && d.Descendants("chapter").Count() > 0)
                              select new Book
                              {
                                  Author = d.Attribute("Author").Value,
                                  Title = d.Attribute("Title").Value,
                                  Chapters = this.GetChapters(d)
                              });

            this.MyTree.ItemsSource = dataSource.ToList();
            NodeLayout mylayout = new NodeLayout();
            mylayout.Key = "BookLayout";
            mylayout.TargetTypeName = "Book";
            mylayout.DisplayMemberPath = "Title";


            NodeLayout mylayout2 = new NodeLayout();
            mylayout2.Key = "ChapterLayout";
            mylayout2.TargetTypeName = "Chapter";
            mylayout2.DisplayMemberPath = "Title";

            MyTree.GlobalNodeLayouts.Add(mylayout);
            MyTree.GlobalNodeLayouts.Add(mylayout2);

            //TODO: Enable check boxes
            //MyTree.CheckBoxSettings.CheckBoxVisibility = Visibility.Visible;
            
            //TODO: Enable Node Lines
            //MyTree.NodeLineVisibility = Visibility.Visible;
            
            //TODO: Enable Selection
            //MyTree.SelectionSettings.NodeSelection = TreeSelectionType.Multiple;
            
            //TODO: Enable Editing
            //MyTree.EditingSettings.AllowEditing = true;

            LayoutRoot.Children.Add(MyTree);
            Grid.SetRow(MyTree, 2);
        }

        private IEnumerable<Chapter> GetChapters(XElement parent)
        {
            return (from d in parent.Descendants("chapter")
                    select new Chapter
                    {
                        Title = d.Attribute("Title").Value
                    }).ToList<Chapter>();
        }

    }
}